from Modules.mod_psql import PSQLDB
import logging
import time


class Logging:
    logging.basicConfig(filename="result.log", level=logging.INFO, encoding='utf8')

    def set_log(self, connection, info: str, type_info: str, name_schema: str):
        named_tuple = time.localtime()
        info = info.replace("'", "")
        sql: str = f"""INSERT INTO {name_schema}.LOG VALUES (to_timestamp('{time.strftime("%d/%m/%Y, %H:%M:%S", named_tuple)}', 
                            'dd/mm/yyyy hh24:mi:ss'), '{type_info}', '{"', '".join(info.split(";"))}')"""
        PSQLDB().insert(connection, sql)

    def write_log_to_file(self, info: str, type_info: str):
        match type_info:
            case 'error':
                named_tuple = time.localtime()
                logging.error(f'[{time.strftime("%d/%m/%Y, %H:%M:%S", named_tuple)}] {info}')
            case 'info':
                named_tuple = time.localtime()
                logging.info(f'[{time.strftime("%d/%m/%Y, %H:%M:%S", named_tuple)}] {info}')
            case _:
                ...
